<?php

namespace backend\models;

use Yii;

/**
 * This is the model class for table "sms_code".
 *
 * @property string $id
 * @property string $phone 发送手机号
 * @property string $sms_code 短信验证码
 * @property int $status 状态,0无效1有效
 * @property string $create_time 发送时间
 */
class SmsCodeModel extends \backend\base\BaseModel
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'sms_code';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['phone','sms_code'], 'required','on'=>'add'],
            [['status', 'create_time'], 'integer'],
            [['phone'], 'string', 'max' => 11],
            [['sms_code'], 'string', 'max' => 6],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'phone' => '手机号',
            'sms_code' => '短信验证码',
            'status' => '状态',
            'create_time' => '发送时间',
        ];
    }

    /**
     * 保存前
     * @param bool $insert
     * @return bool
     */
    public function beforeSave($insert)
    {
        if(parent::beforeSave($insert)){
            $time = time();
            if($this->isNewRecord){
                $this->create_time = $time;
                $this->status = 1;
            }
            return true;
        }
        return false;
    }
}
